/*******************************************************************************
  * Copyright(c) 2023 Levetop Semiconductor Co.,Led. All rights reserved.
  * @file     button.c
  * @author   UartTFT Application Team
  * @version  V0.0.1
  * @date     2023-01-04
  * @brief    This file contains the functions prototypes for the UartTFT
  *           Application display firmware.
 ********************************************************************************/
#include "button.h"
#include "data.h"
#include "bsp.h"

#include "uiIcon.h"
#include "popupBox.h"
#include "levetop.h"
#include "transport.h"
#include "uart.h"
#include "function_drv.h"


/*basic touch*/
uint8_t Basci_count = 0;
uint8_t Basci_flag = 0;
uint8_t Basci_num = 0;
Basci_Info gBasci_Info[BASIC_SIZE];

/***********************************************************************************
 * Function Name : Basic_touch
 * Description   : Touch detection of Basic_touch.
 * Input         : None
 * Output        : None
 * Return        : None
 ************************************************************************************/
uint8_t anjian_flag = 0;
uint8_t rebuf[20];
int Play_pause=0;
uint8_t Order_flag=0;
uint32_t Order_timesum=0;
uint8_t up_down_flag=0;
uint32_t up_down_timesum=0;
static const int varAddr[] = {0x0AB1*2+1,0x0AB2*2+1,0x0AB3*2+1,0x0AB4*2+1,0x0AB5*2+1};

void send_data_frame1(int value){
    rebuf[3] = 0xFF;
    rebuf[4] = 0x00;
    rebuf[5] = 0x00;
    rebuf[6] = value;
    rebuf[7] = 0xBB;
    LT_SendData_CRC_Frame(rebuf, 5);
}
void send_data_frame2(int value){
    rebuf[3] = 0x02;
    rebuf[4] = value;
    rebuf[5] = 0x00;
	rebuf[6] = 0X00;
	rebuf[7] = 0xBB;
	LT_SendData_CRC_Frame(rebuf, 5);
    var[0x0400*2+1]=value;
}


int Icon_flag=0;
int Usb_flag=1;
void Basic_touch(void)
{
	uint8_t i = 0;
	uint8_t rebuf[20] , buff1[12];
	uint16_t w,h,flag;
	if (gTpInfo.sta) // press down
	{
		for (i = 0; i < Basci_count; i++)
		{
			if (gTpInfo.x[0] >= gBasci_Info[i].Xs && gTpInfo.y[0] >= gBasci_Info[i].Ys &&
				gTpInfo.x[0] <= gBasci_Info[i].Xe && gTpInfo.y[0] <= gBasci_Info[i].Ye &&
				Basci_flag == 0 && button_Press_flag == 0)
			{
				if (gBasci_Info[i].press_id != 0xFFFF)
				{
					if (gBasci_Info[i].overlay == 0xFFFF)
						Display_Icon(gBasci_Info[i].press_id, gBasci_Info[i].Xs, gBasci_Info[i].Ys, 0);
					else
					{
						LT_ReadFlash_UI(buff1, addr_index[10] + 12 * gBasci_Info[i].press_id  + multi_language * 12 * addr_index[11], 12);
						pic_add = (buff1[0]) + (buff1[1] << 8) + (buff1[2] << 16) + (buff1[3] << 24);
						w = buff1[4] + (buff1[5] << 8);
						h = buff1[6] + (buff1[7] << 8);
						pic_len = buff1[8] + (buff1[9] << 8) + (buff1[10] << 16);
						flag = buff1[11];


						if (flag == 1 || flag == 2)
							LT268_BG_to_buffer(0, 0, w, m_PngData,gBasci_Info[i].Xs, gBasci_Info[i].Ys,w, h);

						LT268_pic_to_buffer(0,0,w,m_PngData,
											0,0,w,pic_add,
											w,h,flag);

						Get_date_variable_Id_overlay(pic_id,gBasci_Info[i].overlay,gBasci_Info[i].Xs,gBasci_Info[i].Ys,w,h);


						LT268_buffer_to_TFT(0,0,w,m_PngData,
								gBasci_Info[i].Xs,gBasci_Info[i].Ys,w,h);
					}
				}
				Basci_num = i;
				Basci_flag = 1;
				button_Press_flag = 1;
				if (buzzer)
					touch_buzzer();
				if(anjian_flag==0)
				{
					anjian_flag=1;
					//LTPrintf("down\n");
				}
				if(gBasci_Info[Basci_num].Keyvalue==0x401)
				{
					up_down_flag=1;
				}
				if(gBasci_Info[Basci_num].Keyvalue==0x402)
				{
					up_down_flag=1;
				}
			}
		}
	}

	if (Button_Key_Code_Flag == 1)
	{
		for (i = 0; i < Basci_count; i++)
		{
			if (gBasci_Info[i].Xs == 0xFFFF && gBasci_Info[i].Ys ==  Button_Key_Code)
			{

				Button_Key_Code_Flag = 0;
				Button_Key_Code = 0;
				if (gBasci_Info[i].Code == 0xC001)
				{
					rebuf[3] = 0x41;
					rebuf[4] = 0xFF;
					rebuf[5] = 0xFF;
					rebuf[6] = gBasci_Info[i].Keyvalue >> 8;
					rebuf[7] = gBasci_Info[i].Keyvalue & 0xFF;
					LT_SendData_CRC_Frame(rebuf, 5);
				}

				if (gBasci_Info[i].Next_id != 0xFFFF)
				{
					Display_page(gBasci_Info[i].Next_id);
				}
				break;
			}
		}
	}

	// raise
	if (gTpInfo.sta == 0 && Basci_flag == 1)
	{
		if(anjian_flag==1)
		{
			if(gBasci_Info[Basci_num].Keyvalue==0xE08)
			{
				File_level++;
			    if (File_level > set_Filelevel)
			    	File_level = set_Filelevel;
			     Display_page(13);
			     Page_sum1=1;
			     var[0x0EA0*2+1] = File_level;
			     send_data_ALL(0x08,(Page_sum1 >> 8) & 0xFF,Page_sum1 & 0xFF,File_level,0xBB);
				 for (int i = 0; i < 5; i++) {
				     int addr = 0x1011 + i * 0x20;
				     var[addr * 2] = 0;
				     var[addr * 2 + 1] = 0;
				 }
			}
			if((gBasci_Info[Basci_num].Keyvalue==0x71A)&&Set_flag!=1)//page7   //7_1
			{
				switch (Set_flag) {
					case 2:
						Display_page(8);
						break;
					default:
						break;
				}
			}
			if(gBasci_Info[Basci_num].Keyvalue==0x71B)//page7   //7_2
			{
				switch (Set_flag) {
					case 1:
						Display_page(8);
						break;
					case 2:
						Display_page(9);
						break;
					default:
						break;
				}
			}
			if((gBasci_Info[Basci_num].Keyvalue==0x71C)&&Set_flag!=2)//page7   //7_3
			{
				switch (Set_flag) {
					case 1:
						Display_page(9);
						break;
					default:
						break;
				}
			}
			if(gBasci_Info[Basci_num].Keyvalue==0x40A)
			{
				Play_pause++;
				 if(Play_pause>1)
					 Play_pause=0;
					var[0x020C*2+1] = Play_pause;
					var[0x0403*2+1] = var[0x020C*2+1];
					var[0x0403*2+1] = Play_pause;
					rebuf[3] = 0x03;
					rebuf[4] = Play_pause;
					rebuf[5] = 0x00;
					rebuf[6] = 0X00;
					rebuf[7] = 0xBB;
					LT_SendData_CRC_Frame(rebuf, 5);
			}
			if(gBasci_Info[Basci_num].Keyvalue==0x451)
			{
			  Display_page(5);
			  fanhui_flag=0;
			}
			if(gBasci_Info[Basci_num].Keyvalue==0x0450)
			{
				Play_Order++;
				 if(Play_Order>4)
					 Play_Order=1;
					var[0x0404*2+1] = Play_Order;
					var[0x0407*2+1] = Play_Order;
					Order_flag=1;
					Order_timesum=0;
					rebuf[3] = 0x04;
					rebuf[4] = Play_Order;
					rebuf[5] = 0x00;
					rebuf[6] = 0X00;
					rebuf[7] = 0xBB;
					LT_SendData_CRC_Frame(rebuf, 5);
			}
			if(gBasci_Info[Basci_num].Keyvalue==0x401)
			{
				if(Long_flag==0)
				{
					rebuf[3] = 0x05;
					rebuf[4] = 0x01;
					rebuf[5] = 0x00;
					rebuf[6] = 0X00;
					rebuf[7] = 0xBB;
					LT_SendData_CRC_Frame(rebuf, 5);
				}
				else
				{
					rebuf[3] = 0x06;
					rebuf[4] = 0x00;
					rebuf[5] = 0x00;
					rebuf[6] = 0X00;
					rebuf[7] = 0xBB;
					LT_SendData_CRC_Frame(rebuf, 5);
					Long_flag=0;
				}
				up_down_flag=0;
				up_down_timesum=0;
			}
			if(gBasci_Info[Basci_num].Keyvalue==0x402)
			{
				if(Long_flag==0)
				{
					rebuf[3] = 0x05;
					rebuf[4] = 0x02;
					rebuf[5] = 0x00;
					rebuf[6] = 0X00;
					rebuf[7] = 0xBB;
					LT_SendData_CRC_Frame(rebuf, 5);
				}
				else
				{
					rebuf[3] = 0x06;
					rebuf[4] = 0x00;
					rebuf[5] = 0x00;
					rebuf[6] = 0X00;
					rebuf[7] = 0xBB;
					LT_SendData_CRC_Frame(rebuf, 5);
					Long_flag=0;
				}
				up_down_flag=0;
				up_down_timesum=0;
			}
				/*if((gBasci_Info[Basci_num].Keyvalue==0x801)&&Menu_2_flag!=1&&Menu_2_flag!=2)//page2 //2_1
				{
					//Display_page(3);
					var[0x0471*2+1] = 10;
					var[0x0472*2+1] = 1;
					for (int i = 0; i < 5; i++) {
						var[varAddr[i]] = 0;
					}
					var[varAddr[0]]=1;
					switch (Menu_2_flag) {
						case 3:
							Usb_flag=1;
							send_data_frame2(0x01);
							Highlighted_state=3;
							break;
						case 4:
							Usb_flag=1;
							send_data_frame2(0x02);
							Highlighted_state=4;
							break;
						case 5:
							Usb_flag=1;
							send_data_frame2(0x03);
							Highlighted_state=5;
							break;
						case 6:
							Usb_flag=0;
							send_data_frame2(0x04);
							Highlighted_state=6;
							break;
						case 7:
							Usb_flag=0;
							send_data_frame2(0x05);
							Highlighted_state=7;
							break;
						case 8:
							Usb_flag=0;
							send_data_frame2(0x06);
							Highlighted_state=8;
							break;
						case 9:
							Usb_flag=0;
							send_data_frame2(0x07);
							Highlighted_state=9;
							break;
						default:
							break;
					}
				}
				if((gBasci_Info[Basci_num].Keyvalue==0x802)&&Menu_2_flag!=1)//2_2
				{
					//Display_page(3);
					var[0x0471*2+1] = 10;
					var[0x0472*2+1] = 1;
					for (int i = 0; i < 5; i++) {
						var[varAddr[i]] = 0;
					}
					var[varAddr[1]]=1;
					switch (Menu_2_flag) {
						case 2:
							Usb_flag=1;
							send_data_frame2(0x01);
							Highlighted_state=2;
							break;
						case 3:
							Usb_flag=1;
							send_data_frame2(0x02);
							Highlighted_state=3;
							break;
						case 4:
							Usb_flag=1;
							send_data_frame2(0x03);
							Highlighted_state=4;
							break;
						case 5:
							Usb_flag=0;
							send_data_frame2(0x04);
							Highlighted_state=5;
							break;
						case 6:
							Usb_flag=0;
							send_data_frame2(0x05);
							Highlighted_state=6;
							break;
						case 7:
							Usb_flag=0;
							send_data_frame2(0x06);
							Highlighted_state=7;
							break;
						case 8:
							Usb_flag=0;
							send_data_frame2(0x07);
							Highlighted_state=8;
							break;
						case 9:
							Usb_flag=0;
							send_data_frame2(0x08);
							Highlighted_state=9;
							break;
						default:
							break;
					}
				}*/
				if(gBasci_Info[Basci_num].Keyvalue==0x803)//2_3
				{
					//Display_page(3);
					var[0x0471*2+1] = 10;
					var[0x0472*2+1] = 1;
					Highlighted_state=Menu_2_flag;
					for (int i = 0; i < 5; i++) {
						var[varAddr[i]] = 0;
					}
					var[varAddr[2]]=1;
					switch (Menu_2_flag) {
						case 1:
							Usb_flag=1;
							send_data_frame2(0x01);
							break;
						case 2:
							Usb_flag=1;
							send_data_frame2(0x02);
							break;
						case 3:
							Usb_flag=1;
							send_data_frame2(0x03);
							break;
						case 4:
							Usb_flag=0;
							send_data_frame2(0x04);
							break;
						case 5:
							Usb_flag=0;
							send_data_frame2(0x05);
							break;
						case 6:
							Usb_flag=0;
							send_data_frame2(0x06);
							break;
						case 7:
							Usb_flag=0;
							send_data_frame2(0x07);
							break;
						case 8:
							Usb_flag=0;
							send_data_frame2(0x08);
							break;
						case 9:
							Usb_flag=0;
							send_data_frame2(0x09);
							break;
						default:
							break;
					}
				}
				/*if((gBasci_Info[Basci_num].Keyvalue==0x804)&&Menu_2_flag!=9)//2_4
				{
					//Display_page(3);
					var[0x0471*2+1] = 10;
					var[0x0472*2+1] = 1;
					Highlighted_state=Menu_2_flag;
					for (int i = 0; i < 5; i++) {
						var[varAddr[i]] = 0;
					}
					var[varAddr[3]]=1;
					switch (Menu_2_flag) {
						case 1:
							Usb_flag=1;
							send_data_frame2(0x02);
							Highlighted_state=1;
							break;
						case 2:
							Usb_flag=1;
							send_data_frame2(0x03);
							Highlighted_state=2;
							break;
						case 3:
							Usb_flag=0;
							send_data_frame2(0x04);
							Highlighted_state=3;
							break;
						case 4:
							Usb_flag=0;
							send_data_frame2(0x05);
							Highlighted_state=4;
							break;
						case 5:
							Usb_flag=0;
							send_data_frame2(0x06);
							Highlighted_state=5;
							break;
						case 6:
							Usb_flag=0;
							send_data_frame2(0x07);
							Highlighted_state=6;
							break;
						case 7:
							Usb_flag=0;
							send_data_frame2(0x08);
							Highlighted_state=7;
							break;
						case 8:
							Usb_flag=0;
							send_data_frame2(0x09);
							Highlighted_state=8;
							break;
						default:
							break;
					}
				}
				if((gBasci_Info[Basci_num].Keyvalue==0x805)&&Menu_2_flag!=8&&Menu_2_flag!=9)//2_5
				{
					//Display_page(3);
					var[0x0471*2+1] = 10;
					var[0x0472*2+1] = 1;
					Highlighted_state=Menu_2_flag;
					for (int i = 0; i < 5; i++) {
						var[varAddr[i]] = 0;
					}
					var[varAddr[4]]=1;
					switch (Menu_2_flag) {
						case 1:
							Usb_flag=1;
							send_data_frame2(0x03);
							Highlighted_state=1;
							break;
						case 2:
							Usb_flag=0;
							send_data_frame2(0x04);
							Highlighted_state=2;
							break;
						case 3:
							Usb_flag=0;
							send_data_frame2(0x05);
							Highlighted_state=3;
							break;
						case 4:
							Usb_flag=0;
							send_data_frame2(0x06);
							Highlighted_state=4;
							break;
						case 5:
							Usb_flag=0;
							send_data_frame2(0x07);
							Highlighted_state=5;
							break;
						case 6:
							Usb_flag=0;
							send_data_frame2(0x08);
							Highlighted_state=6;
							break;
						case 7:
							Usb_flag=0;
							send_data_frame2(0x09);
							Highlighted_state=7;
							break;
						default:
							break;
					}
				}*/

				if((gBasci_Info[Basci_num].Keyvalue==0x701)&&Menu_1_flag!=1&&Menu_1_flag!=2)//page1   //1_1
				{
					switch (Menu_1_flag) {
						case 3:
							//send_data_frame1(0x01);
							Display_page(13);
							File_level = 1;
							Page_sum1=1;
							var[0x0EA0*2+1] = 1;
							send_data_ALL(0x08,(Page_sum1 >> 8) & 0xFF,Page_sum1 & 0xFF,File_level,0xBB);
							break;
						case 4:
							if(Usb_flag==1)
							{
							 Display_page(4);
							}
							else
							 Display_page(6);
							//send_data_frame1(0x02);
							break;
						case 5:
							Display_page(2);
							//send_data_frame1(0x03);
							break;
						default:
							break;
					}
				}
				if((gBasci_Info[Basci_num].Keyvalue==0x702)&&Menu_1_flag!=1)//1_2
				{
					switch (Menu_1_flag) {
						case 2:
							//send_data_frame1(0x01);
							Display_page(13);
							File_level = 1;
							Page_sum1=1;
							var[0x0EA0*2+1] = 1;
							send_data_ALL(0x08,(Page_sum1 >> 8) & 0xFF,Page_sum1 & 0xFF,File_level,0xBB);
							break;
						case 3:
							if(Usb_flag==1)
							{
							 Display_page(4);
							}
							else
							 Display_page(6);
							//send_data_frame1(0x02);
							break;
						case 4:
							Display_page(2);
							//send_data_frame1(0x03);
							break;
						case 5:
							 Display_page(7);
							//send_data_frame1(0x04);
							break;
						default:
							break;
					}
				}
				if(gBasci_Info[Basci_num].Keyvalue==0x703)//1_3
				{
					switch (Menu_1_flag) {
						case 1:
							//send_data_frame1(0x01);
							Display_page(13);
							File_level = 1;
							Page_sum1=1;
							var[0x0EA0*2+1] = 1;
							send_data_ALL(0x08,(Page_sum1 >> 8) & 0xFF,Page_sum1 & 0xFF,File_level,0xBB);
							break;
						case 2:
							if(Usb_flag==1)
							{
							 Display_page(4);
							}
							else
							 Display_page(6);
							//send_data_frame1(0x02);
							break;
						case 3:
							Display_page(2);
							//send_data_frame1(0x03);
							break;
						case 4:
							 Display_page(7);
							//send_data_frame1(0x04);
							break;
						case 5:
							 Display_page(10);
							//send_data_frame1(0x05);
							break;
						default:
							break;
					}
				}
				if((gBasci_Info[Basci_num].Keyvalue==0x704)&&Menu_1_flag!=5)//1_4
				{
					switch (Menu_1_flag) {
						case 1:
							if(Usb_flag==1)
							{
							Display_page(4);
							}
							else
							Display_page(6);
							//send_data_frame1(0x02);
							break;
						case 2:
							Display_page(2);
							//send_data_frame1(0x03);
							break;
						case 3:
							Display_page(7);
							//send_data_frame1(0x04);
							break;
						case 4:
							 Display_page(10);
							//send_data_frame1(0x05);
							break;
						default:
							break;
					}
				}
				if((gBasci_Info[Basci_num].Keyvalue==0x705)&&Menu_1_flag!=4&&Menu_1_flag!=5)//1_5
				{
					switch (Menu_1_flag) {
						case 1:
							Display_page(2);
							//send_data_frame1(0x03);
							break;
						case 2:
							Display_page(7);
							//send_data_frame1(0x04);
							break;
						case 3:
							Display_page(10);
							//send_data_frame1(0x05);
							break;
						default:
							break;
					}
				}
				anjian_flag=0;
			}
		if (gBasci_Info[Basci_num].Code == 0xC001)
		{
			rebuf[3] = 0x41;
			rebuf[4] = 0xFF;
			rebuf[5] = 0xFF;
			rebuf[6] = gBasci_Info[Basci_num].Keyvalue >> 8;
			rebuf[7] = gBasci_Info[Basci_num].Keyvalue & 0xFF;
			LT_SendData_CRC_Frame(rebuf, 5);
		}
		if (gBasci_Info[Basci_num].id != 0xFFFF)
		{
			if (gBasci_Info[Basci_num].overlay == 0xFFFF)
				Display_Icon(gBasci_Info[Basci_num].id, gBasci_Info[Basci_num].Xs, gBasci_Info[Basci_num].Ys, 0);
			else
			{
				LT_ReadFlash_UI(buff1, addr_index[10] + 12 * gBasci_Info[Basci_num].id  + multi_language * 12 * addr_index[11], 12);
				pic_add = (buff1[0]) + (buff1[1] << 8) + (buff1[2] << 16) + (buff1[3] << 24);
				w = buff1[4] + (buff1[5] << 8);
				h = buff1[6] + (buff1[7] << 8);
				pic_len = buff1[8] + (buff1[9] << 8) + (buff1[10] << 16);
				flag = buff1[11];

				if (flag == 1 || flag == 2)
					LT268_BG_to_buffer(0, 0, w, m_PngData,gBasci_Info[Basci_num].Xs, gBasci_Info[Basci_num].Ys,w, h);

				LT268_pic_to_buffer(0,0,w,m_PngData,
									0,0,w,pic_add,
									w,h,flag);

				Get_date_variable_Id_overlay(pic_id,gBasci_Info[Basci_num].overlay,gBasci_Info[Basci_num].Xs,gBasci_Info[Basci_num].Ys,w,h);


				LT268_buffer_to_TFT(0,0,w,m_PngData,
						gBasci_Info[Basci_num].Xs,gBasci_Info[Basci_num].Ys,w,h);
			}
		}
		Basci_flag = 0;
		button_Press_flag = 0;
		if (gBasci_Info[Basci_num].Next_id != 0xFFFF)
		{
			Display_page(gBasci_Info[Basci_num].Next_id);
		}
	}
}


void Basci_touch_overlay(uint16_t id, uint8_t num)
{
	uint16_t w, h, flag, icon_id;
	uint8_t buff1[12];
	if(gBasci_Info[num].overlay != 0xFFFF)
	{
		if(Get_date_variable_Id_overlay_update(id,gBasci_Info[num].overlay))
		{
			if(Basci_flag == 1) icon_id = gBasci_Info[num].press_id;
			else icon_id = gBasci_Info[num].id;

			if (icon_id == 0xFFFF)
				icon_id = gBasci_Info[num].press_id;

			if (icon_id != 0xFFFF)
			{
				LT_ReadFlash_UI(buff1, addr_index[10] + 12 * icon_id + multi_language * 12 * addr_index[11], 12);
				pic_add = (buff1[0]) + (buff1[1] << 8) + (buff1[2] << 16) + (buff1[3] << 24);
				w = buff1[4] + (buff1[5] << 8);
				h = buff1[6] + (buff1[7] << 8);
				pic_len = buff1[8] + (buff1[9] << 8) + (buff1[10] << 16);
				flag = buff1[11];


				if (flag == 1 || flag == 2)
					LT268_BG_to_buffer(0, 0, w, m_PngData,gBasci_Info[num].Xs, gBasci_Info[num].Ys,w, h);

				LT268_pic_to_buffer(0,0,w,m_PngData,
									0,0,w,pic_add,
									w,h,flag);

				Get_date_variable_Id_overlay(pic_id,gBasci_Info[num].overlay,gBasci_Info[num].Xs,gBasci_Info[num].Ys,w,h);


				LT268_buffer_to_TFT(0,0,w,m_PngData,
						gBasci_Info[num].Xs,gBasci_Info[num].Ys,w,h);
			}
		}
	}
}

